/***************************************************************************
 *   Copyright (C) 2007 by Warren Kenny   *
 *   warren.kenny@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include <otaIPHeader.h>

otaIPHeader::otaIPHeader()
{
	
}

otaIPHeader::otaIPHeader( unsigned char version, 
			  unsigned char typeOfService, 
     			  unsigned short totalLength,
	  		  unsigned short identification,
       			  unsigned short fragmentationOffset,
	    		  unsigned char timeToLive,
	 		  unsigned char protocol,
      			  unsigned short checksum,
	   		  unsigned long sourceIP,
			  unsigned long	destinationIP )
: m_version(version),
  m_typeOfService(typeOfService),
  m_totalLength(totalLength),
  m_identification(identification),
  m_fragmentationOffset(fragmentationOffset),
  m_timeToLive(timeToLive),
  m_protocol(protocol),
  m_checksum(checksum),
  m_sourceIP(sourceIP),
  m_destinationIP(destinationIP)
{
}

bool otaIPHeader::deserialize( const char *data, const unsigned int dataSize, const unsigned int offset, unsigned int &newOffset )
{
	if( dataSize - offset < OTA_IP_HEADER_LENGTH )
	{
		return false;
	}
	else
	{
		bool ok = true;
		
		ok = (	otaBytePacker::removeBasicType( data, dataSize, offset, newOffset, m_version ) &&
			otaBytePacker::removeBasicType( data, dataSize, newOffset, newOffset,  m_typeOfService) &&
			otaBytePacker::removeBasicType( data, dataSize, newOffset, newOffset,  m_totalLength) &&
			otaBytePacker::removeBasicType( data, dataSize, newOffset, newOffset, m_identification ) &&
			otaBytePacker::removeBasicType( data, dataSize, newOffset, newOffset, m_fragmentationOffset ) &&
			otaBytePacker::removeBasicType( data, dataSize, newOffset, newOffset, m_timeToLive ) &&
			otaBytePacker::removeBasicType( data, dataSize, newOffset, newOffset, m_protocol ) &&
			otaBytePacker::removeBasicType( data, dataSize, newOffset, newOffset, m_checksum ) &&
			otaBytePacker::removeBasicType( data, dataSize, newOffset, newOffset, m_sourceIP ) &&
			otaBytePacker::removeBasicType( data, dataSize, newOffset, newOffset, m_destinationIP ) );
		
		return ok;
	}
}
    
bool otaIPHeader::serialize( char *data, const unsigned int dataSize, const unsigned int offset, unsigned int &newOffset )
{
	if( dataSize - offset >= OTA_IP_HEADER_LENGTH )
	{
		return false;
	}
	else
	{
		bool ok =( 	otaBytePacker::appendBasicType( data, dataSize, offset, newOffset, m_version ) &&
				otaBytePacker::appendBasicType( data, dataSize, newOffset, newOffset, m_typeOfService ) &&
				otaBytePacker::appendBasicType( data, dataSize, newOffset, newOffset, m_totalLength ) &&
				otaBytePacker::appendBasicType( data, dataSize, newOffset, newOffset, m_identification ) &&
				otaBytePacker::appendBasicType( data, dataSize, newOffset, newOffset, m_fragmentationOffset ) &&
				otaBytePacker::appendBasicType( data, dataSize, newOffset, newOffset, m_timeToLive ) &&
				otaBytePacker::appendBasicType( data, dataSize, newOffset, newOffset, m_protocol ) &&
				otaBytePacker::appendBasicType( data, dataSize, newOffset, newOffset, m_checksum ) &&
				otaBytePacker::appendBasicType( data, dataSize, newOffset, newOffset, m_sourceIP ) &&
				otaBytePacker::appendBasicType( data, dataSize, newOffset, newOffset, m_destinationIP ) );
						
		return ok;
	}
}
	
unsigned long otaIPHeader::getSourceIP()
{
	return m_sourceIP;
}
	
unsigned long otaIPHeader::getDestinationIP()
{
	return m_destinationIP;
}

unsigned char otaIPHeader::getProtocol()
{
	return m_protocol;
}

unsigned char otaIPHeader::getHeaderLength()
{
	return (m_version & 0x0F);
}

otaIPHeader::~otaIPHeader()
{
}


